//

// (  )
//

package com.tools.acceleratecore.xlib.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.tools.acceleratecore.xlib.util.UtilRoot.IRootCommand;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

class RootCommand implements IRootCommand {
    private List<String> listSuPackageName = new ArrayList();
    private static final String VALUE_STRING_DEFAULT_CHARSET = "utf-8";

    RootCommand() {
        this.listSuPackageName.add("com.noshufou.android.su");
        this.listSuPackageName.add("com.qihoo.root");
        this.listSuPackageName.add("com.lbe.security.miui");
        this.listSuPackageName.add("com.lbe.security.su");
        this.listSuPackageName.add("com.lbe.security.shuame");
        this.listSuPackageName.add("eu.chainfire.supersu");
        this.listSuPackageName.add("com.miui.uac");
    }

    public boolean checkRoot() {
        return this.isRoot();
    }

    public String doCommand(String strCmd) {
        return this.execOnRoot(new File("/"), strCmd);
    }

    private String findSuShellBin() {
        String strPath = System.getenv("PATH");
        if (TextUtils.isEmpty(strPath)) {
            return null;
        } else {
            String[] arraystr = strPath.split(":");
            String[] var6 = arraystr;
            int var5 = arraystr.length;

            for (int var4 = 0; var4 < var5; ++var4) {
                String str = var6[var4];
                File file = new File(str, "su");
                if (file.exists()) {
                    return file.getPath();
                }
            }

            return null;
        }
    }

    private String findShShellBin() {
        String strPath = System.getenv("PATH");
        if (TextUtils.isEmpty(strPath)) {
            return null;
        } else {
            String[] arraystr = strPath.split(":");
            String[] var6 = arraystr;
            int var5 = arraystr.length;

            for (int var4 = 0; var4 < var5; ++var4) {
                String str = var6[var4];
                File file = new File(str, "sh");
                if (file.exists()) {
                    return file.getPath();
                }
            }

            return null;
        }
    }

    private String readStrFromInputStream(InputStream in, String charset) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(in, charset));
            char[] buffer = new char[UtilEnv.VALUE_INT_BUFFER_SIZE];
            int nRead ;
            StringBuilder builder = new StringBuilder();

            while ((nRead = br.read(buffer)) != -1) {
                builder.append(buffer, 0, nRead);
            }

            return builder.length() > 0 ? builder.toString() : null;
        } catch (Exception var7) {
            var7.printStackTrace();
            return null;
        }
    }

    private String exec(File directory, String... strCmds) {
        Process process = null;

        String var9;
        try {
            ProcessBuilder pb = (new ProcessBuilder(new String[0])).command(strCmds).redirectErrorStream(true).directory(directory);
            pb.environment().putAll(System.getenv());
            process = pb.start();
            InputStream stdOut = process.getInputStream();
            String str = this.readStrFromInputStream(stdOut, "utf-8");
            stdOut.close();
            if (str != null) {
                var9 = str;
                return var9;
            }

            InputStream stdError = process.getErrorStream();
            var9 = this.readStrFromInputStream(stdError, "utf-8");
        } catch (Exception var12) {
            var12.printStackTrace();
            return "unknow";
        } finally {
            if (process != null) {
                process.destroy();
            }

        }

        return var9;
    }

    private void exec(String strCmd) {
        try {
            Process p = Runtime.getRuntime().exec(strCmd);
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

            while (stdInput.readLine() != null) {
                ;
            }

            p.waitFor();
        } catch (Exception var5) {
            var5.printStackTrace();
        }

    }

    private String execOnRoot(File directory, String... strCmds) {
        Process process = null;
        String su = this.findSuShellBin();
        if (su == null) {
            return null;
        } else {
            String var14;
            try {
                ProcessBuilder pb = (new ProcessBuilder(new String[0])).command(su).redirectErrorStream(true).directory(directory);

                try {
                    pb.environment().putAll(System.getenv());
                } catch (Exception var24) {
                    var24.printStackTrace();
                }

                process = pb.start();
                OutputStream stdIn = process.getOutputStream();
                InputStream stdOut = process.getInputStream();
                InputStream stdError = process.getErrorStream();
                String[] var12 = strCmds;
                int var11 = strCmds.length;

                String str;
                for (int var10 = 0; var10 < var11; ++var10) {
                    str = var12[var10];
                    if (!str.endsWith("\n")) {
                        str = str + "\n";
                    }

                    stdIn.write(str.getBytes());
                    stdIn.flush();
                }

                stdIn.write("exit\n".getBytes());
                stdIn.flush();
                stdIn.close();
                process.waitFor();
                str = this.readStrFromInputStream(stdOut, "utf-8");
                stdOut.close();
                if (str != null) {
                    var14 = str;
                    return var14;
                }

                var14 = this.readStrFromInputStream(stdError, "utf-8");
            } catch (Exception var25) {
                var25.printStackTrace();
                return null;
            } finally {
                if (process != null) {
                    try {
                        process.destroy();
                    } catch (Exception var23) {
                        var23.printStackTrace();
                    }
                }

            }

            return var14;
        }
    }

    private boolean hasSuCmd() {
        return this.findSuShellBin() != null;
    }

    private boolean hasSuApk(Context context) {
        try {
            PackageManager pm = context.getPackageManager();
            Iterator var4 = this.listSuPackageName.iterator();

            while (var4.hasNext()) {
                String strPackageName = (String) var4.next();

                try {
                    PackageInfo info = pm.getPackageInfo(strPackageName, 0);
                    if (info != null) {
                        return true;
                    }
                } catch (Exception var6) {
                    var6.printStackTrace();
                }
            }

            return false;
        } catch (Exception var7) {
            var7.printStackTrace();
            return false;
        }
    }

    private boolean isRoot() {
        try {
            String rootShell = this.findSuShellBin();
            if (rootShell == null) {
                return false;
            } else {
                String str = this.exec(new File("/"), rootShell, "-c", "id");
                if (TextUtils.isEmpty(str)) {
                    return false;
                } else {
                    return str.indexOf("root") != -1;
                }
            }
        } catch (Exception var3) {
            var3.printStackTrace();
            return false;
        }
    }
}
